import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import svgLoader from 'vite-svg-loader'

export default defineConfig({
	plugins: [
		vue({
			// 确保包含所有 Vue 文件
			include: [/\.vue$/],
			// 模板编译选项
			template: {
				transformAssetUrls: {
					// 基础配置
					base: null,
					includeAbsolute: false
				}
			}
		}),
		svgLoader()
	],
	publicDir: false,
	base: './',
	build: {
		target: 'es2015',
		lib: {
			entry: path.resolve(__dirname, './packages/index.js'),
			name: 'template-ui',
			formats: ['es', 'umd'],
			fileName: (format) => `template-ui.${format}.js`
		},
		rollupOptions: {
			external: [
				/\/views\//,
				/\/assets\//,
				// /\/config\//,
				/\/layout\//,
				/\/mock\//,
				/\/router\//,
				/\/locales\//,
				/\/store\//,
				/\/style\//,
				// /\/src\/utils\//,  // 只排除 src/utils，保留 packages/utils
				/\/directive\//,
				'vue',
				'pinia',
				'vue-router',
				'vue-i18n',
				'element-plus',
				'echarts',
				'axios',
				'nprogress',
				// UI 工具
				'@headlessui/vue',
				'@heroicons/vue',
				'splitpanes',
				'sortablejs',
				'vuedraggable',
				'cropperjs',
				// 编辑器相关
				'codemirror',
				'@codemirror/lang-html',
				'@codemirror/lang-javascript',
				'@codemirror/lang-vue',
				'@codemirror/theme-one-dark',
				'tinymce',
				'tinymce/tinymce',
				'@tinymce/tinymce-vue',
				'monaco-editor',
				'ace-builds',
				'vue-codemirror',
				// JS 工具库
				'core-js',
				'lodash',
				'dayjs',
				'jsencrypt',
				'crypto-js',
				'uuid',
				'pinyin-match',
				'jwt-decode',
				// XState / 状态机
				'xstate',
				'@xstate/vue',
				// 其他工具
				'sql-formatter',
				'vanilla-jsoneditor',
				'vue-json-pretty',
				'vue-json-tree',
				'highlight.js',
				'@highlightjs/vue-plugin',
				'xgplayer',
				'xgplayer-hls',
				'xterm',
				'xterm-addon-fit',
				'xterm-addon-webgl',
				'vue3-clipboard',
				// Svelte（如混用）
				'svelte',
				'@xyflow/svelte',
				// TinyMCE + Vue
				'@tinymce/tinymce-vue',
				// 其他（按需添加）
				'@floating-ui/dom',
				'@meetjs/element-plus-kit',
				'@visactor/vtable-sheet',
				'vue3-sfc-loader'
			],
			output: {
				inlineDynamicImports: true, // 禁用分割，所有代码合并到单个文件
				globals: {
					vue: 'Vue',
					'element-plus': 'ElementPlus'
				}
			}
		}
	},

	resolve: {
		extensions: ['.ts', '.vue', '.js', '.json'],
		alias: {
			'@': path.resolve(__dirname, './src'),
			'~': path.resolve(__dirname, './'),
			'#': path.resolve(__dirname, './types'),
			'@@': path.resolve(__dirname, './packages')
		}
	}
})
